// 快速测试问题所在
const BASE_URL = 'http://localhost:5000/api';

async function quickTest() {
  console.log('🔍 快速问题诊断...\n');
  
  // 1. 测试登录
  console.log('1. 测试登录...');
  try {
    const loginResponse = await fetch(`${BASE_URL}/auth/login/email`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        email: 'admin@aide.com',
        password: 'admin123456'
      })
    });
    
    const loginData = await loginResponse.json();
    
    if (loginResponse.ok && loginData.token) {
      console.log('✅ 登录成功');
      const token = loginData.token;
      
      // 2. 测试创建任务
      console.log('\n2. 测试创建任务...');
      const taskResponse = await fetch(`${BASE_URL}/tasks`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${token}`
        },
        body: JSON.stringify({
          title: '测试任务',
          description: '测试描述',
          dueDate: '2024-12-25',
          reminderMinutes: 15,
          priority: 'P2',
          category: 'work'
        })
      });
      
      const taskData = await taskResponse.json();
      
      if (taskResponse.ok) {
        console.log('✅ 任务创建成功');
        console.log(`   任务ID: ${taskData.id}`);
        
        // 3. 验证任务数据
        console.log('\n3. 验证任务数据...');
        const getResponse = await fetch(`${BASE_URL}/tasks/${taskData.id}`, {
          headers: { 'Authorization': `Bearer ${token}` }
        });
        
        const savedTask = await getResponse.json();
        
        if (getResponse.ok) {
          console.log('✅ 任务获取成功');
          console.log(`   due_date: ${savedTask.dueDate}`);
          console.log(`   reminder_minutes: ${savedTask.reminder_minutes}`);
          console.log(`   原始输入: "2024-12-25"`);
          console.log(`   实际保存: "${savedTask.dueDate}"`);
          
          // 检查日期转换是否符合预期
          const isDateOnly = savedTask.dueDate === '2024-12-25';
          const isTimestamp = savedTask.dueDate.includes('T');
          
          console.log(`   是否只有日期: ${isDateOnly}`);
          console.log(`   是否包含时间: ${isTimestamp}`);
          
        } else {
          console.log('❌ 任务获取失败:', savedTask.error);
        }
        
        // 4. 测试提醒检查API
        console.log('\n4. 测试提醒检查API...');
        const reminderResponse = await fetch(`${BASE_URL}/notifications/check-reminders`, {
          method: 'POST',
          headers: { 'Authorization': `Bearer ${token}` }
        });
        
        if (reminderResponse.ok) {
          const reminders = await reminderResponse.json();
          console.log('✅ 提醒检查API成功');
          console.log(`   找到 ${reminders.length} 个提醒`);
          if (reminders.length > 0) {
            console.log('   提醒详情:', reminders[0]);
          }
        } else {
          const errorData = await reminderResponse.json();
          console.log('❌ 提醒检查API失败:', errorData.error);
          console.log('   状态码:', reminderResponse.status);
        }
        
        // 5. 清理测试数据
        await fetch(`${BASE_URL}/tasks/${taskData.id}`, {
          method: 'DELETE',
          headers: { 'Authorization': `Bearer ${token}` }
        });
        console.log('\n✅ 测试数据已清理');
        
      } else {
        console.log('❌ 任务创建失败:', taskData.error);
      }
      
    } else {
      console.log('❌ 登录失败:', loginData.error);
    }
    
  } catch (error) {
    console.log('❌ 测试过程中发生错误:', error.message);
  }
}

quickTest();
